package controller

import (
	"net/http"
	"study_abroad_system_back_end/dao"
	"study_abroad_system_back_end/response"
	"study_abroad_system_back_end/common/StatusCode"
	"study_abroad_system_back_end/models"

	"github.com/gin-gonic/gin"
)

func MajorList(ctx *gin.Context){
	code := StatusCode.SUCCESS

	DB := dao.GetDB()
	var major []models.Major
	DB.Find(&major)
	SortMajor(major)
	response.Response(ctx, http.StatusOK, code, gin.H{"major": major[:10]}, StatusCode.GetMsg(code))
}

func SortMajor(major []models.Major){
	for i := 0; i < len(major); i++{
		for j := 0; j < len(major)-i; j++{
			if j+1<len(major) && major[j].Rank > major[j+1].Rank{
				tmp := major[j]
				major[j] = major[j+1]
				major[j+1] = tmp 
			} 
		}
	}
}